package com.quanquan.user.controller;

import DTO.PageDTO;
import com.quanquan.user.dto.ConsumptionQuotaDTO;
import com.quanquan.user.exception.ConsumptionQuotaException;
import com.quanquan.user.request.ConsumptionQuotaRequestBody;
import com.quanquan.user.service.ConsumptionQuotaService;
import enums.ResCodeEnum;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

/**
 * 功能描述:
 *
 * @ClassName: ConsumptionQuotaController
 * @Author: xiaoqianh丶
 * @Date: 2019-06-06 10:30
 * @Version: V1.0
 */
@RestController
@RequestMapping("/consumption_quota")
public class ConsumptionQuotaController {

    private final ConsumptionQuotaService consumptionQuotaService;

    public ConsumptionQuotaController(ConsumptionQuotaService consumptionQuotaService) {
        this.consumptionQuotaService = consumptionQuotaService;
    }

    @PostMapping("/add")
    public ResultBody createConsumptionQuota (@RequestBody ConsumptionQuotaRequestBody requestBody){
        String consumption = consumptionQuotaService.createConsumption(requestBody);

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", consumption);
    }

    @PostMapping("/query")
    public ResultBody  findAllConsumptionQuota(@RequestBody ConsumptionQuotaRequestBody requestBody){
        PageDTO<ConsumptionQuotaDTO> allConsumption = consumptionQuotaService.findAllConsumption(requestBody);

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", allConsumption);
    }


    @PostMapping("/update")
    public ResultBody updateConsumptionQuota (@RequestBody ConsumptionQuotaRequestBody requestBody){
        String msg = null;
        try {
            msg = consumptionQuotaService.updateConsumptionQuota(requestBody);
        } catch (ConsumptionQuotaException e) {
            e.printStackTrace();
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", msg);
    }


    /**
     * 根据规则查找满额规则
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    @ResponseBody
    public ResultBody queryById(@RequestBody ConsumptionQuotaRequestBody request) {

        ConsumptionQuotaDTO floorDTO = null;
        try {
            floorDTO = consumptionQuotaService.query(request);
        } catch (ConsumptionQuotaException e) {
            e.printStackTrace();
        }

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", floorDTO);
    }

    @PostMapping("/remove")
    public ResultBody removeConsumptionQuota (@RequestBody ConsumptionQuotaRequestBody requestBody){
        String msg = null;
        try {
            msg = consumptionQuotaService.removeConsumptionQuota(requestBody);
        } catch (ConsumptionQuotaException e) {
            e.printStackTrace();
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", msg);
    }
}
